package com.ruoyi.novel.controller;

import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.novel.domain.ChapterDTO;
import com.ruoyi.novel.service.ChapterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 章节Controller
 */
@RestController
@RequestMapping("/chapter")
public class ChapterController extends BaseController {
    @Autowired
    private ChapterService chapterService;

    /**
     * 获取章节内容
     */
    @GetMapping("/content")
    public AjaxResult getContent(@RequestParam("title") String novelName,@RequestParam("orderNum") String orderNum,
                                 @RequestParam("userId") String userId){
        return AjaxResult.success(chapterService.getContent(novelName,orderNum,userId));
    }
    /**
     * 获取章节内容（作者）
     */
    @GetMapping("/contentForAuthor")
    public AjaxResult getContentForAuthor(@RequestParam("title") String novelName,@RequestParam("orderNum") String orderNum){
        return AjaxResult.success(chapterService.getContentForAuthor(novelName,orderNum));
    }
    // 获取下一章
    @GetMapping("/isChapter")
    public AjaxResult getIsChapter(@RequestParam("title") String novelName, @RequestParam("orderNum") String orderNum,
                                   @RequestParam("userId") Long userId) {
        // 返回当前章节信息和下一章信息
        ChapterDTO isChapter = chapterService.getIsChapter(novelName, orderNum,userId);
        if(isChapter == null){
            return AjaxResult.error("没有下一章了");
        }
        return AjaxResult.success(isChapter);
    }
    // 获取上一章
    @GetMapping("/preChapter")
    public AjaxResult getPreChapter(@RequestParam("title") String novelName, @RequestParam("orderNum") String orderNum,
                                    @RequestParam("userId") Long userId) {
        // 返回当前章节信息和上一章信息
        ChapterDTO preChapter = chapterService.getPreChapter(novelName, orderNum,userId);
        if(preChapter == null){
            return AjaxResult.error("欢迎来到第一页");
        }
        return AjaxResult.success(preChapter);
    }
}
